package demoMod.invincibleOne.cards.invincible;

import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.DamageAction;
import com.megacrit.cardcrawl.actions.common.ExhaustSpecificCardAction;
import com.megacrit.cardcrawl.actions.common.GainBlockAction;
import com.megacrit.cardcrawl.actions.watcher.ChangeStanceAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.DamageInfo;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.stances.CalmStance;
import com.megacrit.cardcrawl.stances.WrathStance;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.actions.InvincibleWallopAction;
import demoMod.invincibleOne.enums.CardTagsEnum;

import java.util.List;
import java.util.stream.Collectors;

public class Wallop extends AbstractInvincibleCard {
    public static final String ID = InvincibleOneMod.makeID("Wallop");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/Wallop.png";

    private static final CardType TYPE = CardType.ATTACK;
    private static final CardRarity RARITY = CardRarity.BASIC;
    private static final CardTarget TARGET = CardTarget.ENEMY;

    private static final int COST = 1;

    public Wallop() {
        this(0);
    }

    public Wallop(int upgrades) {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.baseDamage = 12;
        this.timesUpgraded = upgrades;
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> {
            this.upgraded = true;
            this.name = cardStrings.NAME + "+" + this.timesUpgraded;
            initializeTitle();
            upgradeDamage(6);
        };
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        addToBot(new AbstractGameAction() {
            @Override
            public void update() {
                if (AbstractDungeon.player.stance.ID.equals(WrathStance.STANCE_ID)) {
                    addToBot(new ChangeStanceAction(CalmStance.STANCE_ID));
                } else {
                    addToBot(new ChangeStanceAction(WrathStance.STANCE_ID));
                }
                isDone = true;
            }
        });
        addToBot(new InvincibleWallopAction(m, new DamageInfo(p, this.damage, this.damageTypeForTurn), this.uuid));
        addToBot(new AbstractGameAction() {
            @Override
            public void update() {
                List<AbstractCard> cards = p.discardPile.group.stream().filter(c -> (c.type == AbstractCard.CardType.CURSE || c.hasTag(CardTagsEnum.VIRUS))).collect(Collectors.toList());
                if (!cards.isEmpty()) {
                    AbstractCard card = cards.remove(AbstractDungeon.cardRandomRng.random(cards.size() - 1));
                    addToTop(new ExhaustSpecificCardAction(card, p.discardPile, true));
                }
                isDone = true;
            }
        });
    }

    @Override
    public boolean canUpgrade() {
        return true;
    }

    @Override
    public AbstractCard makeCopy() {
        return new Wallop(this.timesUpgraded);
    }
}
